package com.neusoft.yuejian_6.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.javabean.Feedback;
import com.neusoft.yuejian_6.model.FeedbackModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FeedBackActivity extends Activity {


    //声明
    private ImageView back;
    private Button feedback_button;
    private EditText fname, fintro;
    private int user_id;
    private String feed_intro;
    private String feed_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_feed_back);
        initViews();//初始化
        initEvents();//点击事件处理
    }

    private void initViews() {
        fintro = (EditText) findViewById(R.id.feed_intro);
        fname = (EditText) findViewById(R.id.feed_name);
        feedback_button = (Button) findViewById(R.id.feedback_button);
    }

    private void initEvents() {

        feedback_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                user_id = sp.getInt("user_id", 0);
                // Log.d("we","u_id");
                feed_name = fname.getText().toString();
                feed_intro = fintro.getText().toString();
                //  System.out.print(feed_intro);
                FeedbackModel feedbackModel = new FeedbackModel();
                //发送请求
                Call<Feedback> call = feedbackModel.addfeedback
                        (user_id, feed_name, feed_intro);

                //2.处理结果
                call.enqueue(new Callback<Feedback>() {
                    @Override
                    public void onResponse(Call<Feedback> call, Response<Feedback> response) {
                        Feedback data = (Feedback) response.body();  //获取数据
                        //  Log.d("h","u_id");
                        if (data.getSuccess().equals("0")) {
                            Toast.makeText(FeedBackActivity.this, "添加失败", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(FeedBackActivity.this, "添加成功", Toast.LENGTH_LONG).show();
                            Intent intent=new Intent(FeedBackActivity.this, FeedBackActivity.class);
                            startActivity(intent);

                        }
                    }

                    @Override
                    public void onFailure(Call<Feedback> call, Throwable t) {

                    }
                });
//
//                    //3.call and callback 捆绑
//                    call.enqueue(callback);
//                }
            }

        });
    }
}


